
public class TranditionalThreadCommunication {

	/**
	 * 需求：子线程执行10次,然后主线程执行100次...如此交替执行50次
	 */
	public static void main(String[] args) {
		
		final Bussniss bussniss = new Bussniss();
		//子线程
		new Thread(new Runnable(){

			@Override
			public void run() {
				for(int i=1;i<=50;i++){
					bussniss.sub(i);
				}
			}
			
		}).start();
		
		//主线程执行50次
		for(int i=1;i<=50;i++){
			bussniss.main(i);
		}
	}
	
}

class Bussniss {
	boolean bShouldSub = true;
	public synchronized void main(int i){
		while(bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for(int j=1;j<=100;j++){
			System.out.println("main thread squence of "+ j + " loop of "+ i);
		}
		bShouldSub = true;
		this.notify();
	}
	public synchronized void sub(int i){
		while(!bShouldSub){
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for(int j=1;j<=10;j++){
			System.out.println("sub thread squence of "+ j + " loop of "+ i);
		}
		bShouldSub = false;
		this.notify();
	}
}
